package com.sduept.nwld.dataserver.manager.protection;

import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.component.message.model.AlarmReceiveRole;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * 告警消息接收角色相关基础方法接口
 *
 */
@Service
public class AlarmReceiveRoleManager {
	
	@Autowired
	private SuperDAO superDao;
	@Autowired
	private ParamManager paramCache;
	
	/**
	 * 新增、更新
	 * @param entity
	 * @return
	 */
	public AlarmReceiveRole createOrUpdate(AlarmReceiveRole entity) {
		return superDao.createOrUpdate(entity, AlarmReceiveRole.class);
	}
	
	/**
	 * 删除
	 * @param id
	 */
	public void delete(String id) {
		superDao.delete(id, AlarmReceiveRole.class);
	}
	
	/**
	 * 通过消息类型删除
	 * @param alarmType
	 */
	public void deleteByAlarmType(String alarmType) {
		superDao.delete(AlarmReceiveRole.class, "alarmType=? ", alarmType);
	}
	
	/**
	 * 通过消息类型查询
	 * @param alarmType
	 * @return
	 */
	public List<AlarmReceiveRole> findByAlarmType(String alarmType) {
		return superDao.findByCondition(AlarmReceiveRole.class, "alarmType=? ", alarmType);
	}
	
	/**
	 * 查询全部
	 * @return
	 */
	public List<AlarmReceiveRole> findAllAlarmReceiveRole() {
		return superDao.findAll(AlarmReceiveRole.class);
	}
	
	/**
	 * 根据消息类型获取是否全部接收
	 * @param alarmType
	 * @return
	 */
	public Boolean ifAllReceive(String alarmType) {
		Boolean flag = null;
		List<AlarmReceiveRole> list = findByAlarmType(alarmType);
		if(list.size()>0) {
			AlarmReceiveRole bean = list.get(0);
			flag = bean.isAllReceive();
		}
		return flag;
	}
	
	/**
	 * 根据告警消息类型、角色名称或者角色id查询
	 * @param alarmType
	 * @param roleName
	 * @param roleId
	 * @return
	 */
	public List<AlarmReceiveRole> queryByCondition(String alarmType, String roleName, String roleId) {
		SqlBuilder builder = new SqlBuilder();
		builder.buildSelectHeadSql();
		List<Object> whereArgs = new ArrayList<Object>();
		if (StringUtils.isNotEmpty(alarmType)) {
			builder.and().buildAlarmTypeSql();
			whereArgs.add(alarmType);
		}
		if (StringUtils.isNotEmpty(roleName)) {
			builder.and().buildRoleNameSql();
			whereArgs.add(roleName);
		}
		if (StringUtils.isNotEmpty(roleId)) {
			builder.and().buildRoleIdSql();
			whereArgs.add(roleId);
		}
		String sql = builder.build();
		return superDao.nativeQuery(AlarmReceiveRole.class, sql, whereArgs.toArray(new Object[whereArgs.size()]));
	}
	
	/**
	 * 获取所有的消息类型
	 * @return 
	 */
	public List<SystemParameter> initAlarmParams() {
		List<SystemParameter> msgTypes = new ArrayList<SystemParameter>();
		String allTypes = paramCache.get(ParamDesc.RISK_ALARM_TYPES).getValue();
		StringTokenizer token = new StringTokenizer(allTypes, ",");
		while(token.hasMoreTokens()){
			SystemParameter param = paramCache.get(token.nextToken());
			msgTypes.add(param);
		}
		return msgTypes;
	}
	
	/**
	 * 根据消息类型获取接收该消息的角色
	 * @param alarmType
	 * @return
	 */
	public List<String> findAlarmTypeRoleIds(String alarmType) {
		List<AlarmReceiveRole> alarmReceiveRoleList = findByAlarmType(alarmType);
		List<String> roleIds = new ArrayList<String>();
		for (AlarmReceiveRole alarmReceiveRole : alarmReceiveRoleList) {
			if(StringUtils.isNotEmpty(alarmReceiveRole.getRoleId())) {
				roleIds.add(alarmReceiveRole.getRoleId());
			}
		}
		return roleIds;
	}
	
	private final class SqlBuilder {
		private StringBuilder sb = new StringBuilder();

		private String build() {
			return sb.toString();
		}

		public SqlBuilder buildSelectHeadSql() {
			sb.append("SELECT * FROM t_dict_alarmType_role WHERE 1=1");
			return this;
		}

		public SqlBuilder buildAlarmTypeSql() {
			sb.append(" alarmType = ?");
			return this;
		}

		public SqlBuilder buildRoleNameSql() {
			sb.append(" receiveRole = ? ");
			return this;
		}

		public SqlBuilder buildRoleIdSql() {
			sb.append(" roleId = ? ");
			return this;
		}

		public SqlBuilder and() {
			sb.append(" AND ");
			return this;
		}
	}
	
}
